﻿using AutoMapper;
using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Account;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Account
{
    public class AccountDelHandler:IRequestHandler<AccountDelCommand,ApiResult<int>>
    {
        private readonly IRepository<AccountModel> accountway;
        private readonly IRepository<AccountRoleModel> accountroleway;
        private readonly IRepository<RoleModel> roleway;
        private readonly ILogger<LoginHandler> log;
        private readonly IRepository<DepartmeRoleModel> departmeroleway;
        private readonly IRepository<DepartmentModel> departmentway;
        private readonly IMapper mapper;
        private readonly IConfiguration configuration;

        public AccountDelHandler(IRepository<AccountModel> accountway, IRepository<AccountRoleModel>
            accountroleway, IRepository<RoleModel> roleway, ILogger<LoginHandler> log, IRepository<DepartmeRoleModel> departmeroleway, IRepository<DepartmentModel> departmentway, IMapper mapper, IConfiguration configuration)
        {
            this.accountway = accountway;
            this.accountroleway = accountroleway;
            this.roleway = roleway;
            this.log = log;
            this.departmeroleway = departmeroleway;
            this.departmentway = departmentway;
            this.mapper = mapper;
            this.configuration = configuration;
        }
        /// <summary>
        /// 账号软删除
        /// </summary>
        /// <param name="request">主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>影响行数</returns>
        public async Task<ApiResult<int>> Handle(AccountDelCommand request, CancellationToken cancellationToken)
        {
            var res =new  ApiResult<int>();
            var value =await accountway.GetValue(request.AccountId);
            value.IsDel = true;
            var result = await accountway.UpdateAsync(value);
            if(result>0)
            {
                log.LogInformation($"账号:{value.AccountName}删除成功");
                res.Message=$"账号:{value.AccountName}删除成功";
                res.Code = StatusCode.Success;
                res.Data = result;
            }
            else
            {
                res.Message = $"账号{value.AccountName}删除失败";
                res.Code = StatusCode.Fail;
            }
            return res;
        }
    }
}
